(function() {
	window.CHAT = {
		init: function() {
			var that = this;
			
			//用户信息
			var uid = '123';
			var usrname = 'Test-User-Huang';
			
			function generateMixed(n) {
				var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
				var res = "";
				for(var i = 0; i < n ; i ++) {
					var id = Math.ceil(Math.random()*35);
				 	res += chars[id];
				}
				return res;
			}
			
			var pix=generateMixed(3)+'_';
			this.sock_connected=false;
			this.sock_uuid=pix+uid;
			this.sock_name=pix+usrname;
			this.sock_device=pix+"workflow";
			this.live=false;
			
			function connect(){
				//连接websocket后端服务器
				that.sock = io.connect('ws://localhost:8866');
			}
			
			connect();
			
			this.eventBindList = {};
			
			//监听
			//成功连接后触发
			this.sock.on('connect', function (){
				that.sock_connected=true;
				//签名注册
				that.sock.emit('signin',{
					userid:that.sock_uuid,
					device:that.sock_device,
					realname:that.sock_name
				});
			});
			
			this.sock.on('online', function (e) {
				if(e.sockid===that.sock.id){
					that.enableOpenBtn();
				}
				//{uuid:'用户id',sockid:'链接id',data:{userid:'用户id',device:'设备类型',realname:'姓名'}}
				that.infopend(e.data.realname+',上线了.');
			});
			
			this.sock.on('disconnect', function (data) {
				that.infopend('下线了'+JSON.stringify(data));
				that.sock_connected=false;
			});
			
			function heartbeat(){
				live=false;
				that.sock.emit('heartbeat', 'h+');
				//检测15秒后是否返回了消息并更改了在线状态
				setTimeout(function(){
					if(that.live!=true){
						if(that.sock) that.sock.disconnect();
					}
				}, 15 * 1000);//超时15秒断开连接
			}
			
			this.sock.on('heartbeat', function (e) {
				that.live=true;
				//that.infopend('H+');
				console.log('H+');
				setTimeout(function(){
					heartbeat();
				}, 30 * 1000);
			});
			//发送心跳信号
			heartbeat();
		},
		enableOpenBtn:function(){
			$('#openBtn')[0].disabled=false;
		},
		enableCloseBtn:function(){
			$('#closeBtn')[0].disabled=false;
		},
		//反馈信息到页面上
		infopend:function(msg){
			$('#result').prepend(msg+'\r\n');
		},
		open:function(){
			var that = this;
			
			var userId = this.sock_uuid;//$('#userId').val();
			var userName = this.sock_name;//$('#userName').val();
			var fileId = $('#fileId').val();
			var fileName = $('#fileName').val();
			
			//this.$result = $('#result');
			this.userId = userId=='' ? this.getUUid() : userId;
			this.userName = userName=='' ? ('游客'+this.userId) : userName;
			this.fileId = fileId=='' ? ('File'+this.getUUid()) : fileId;
			this.fileName = fileName=='' ? ('临时办件'+this.fileId) : fileName;
			
			this.socket = this.sock;
			
			var obj = {
				fileId: this.fileId,
				fileName: this.fileName,
				userId: this.userId,
				userName: this.userName
			};
			//发送办件信息
			this.socket.emit('fileOpen', obj);
			
			if(this.eventBindList[this.userId]){
				return;
			}
			
			//监听广播
			this.socket.on('fileOpenCallback', function(data){
				//console.log(data);
				if(data.fileId==that.fileId && data.userId==that.userId){
					//console.log(data);
					that.infopend('[OK]办件'+data.fileName+'登录办理中成功');
					that.enableCloseBtn();
				}
			});
			//个人频道监听异常
			this.socket.on('fileOpenCallback?'+that.userId, function(data){
				var obj = data;
				if(obj.userId!==that.userId && obj.fileId==that.fileId){
					//console.log(obj);
					that.infopend('[ERROR]办件'+obj.fileName+'正由'+obj.userName+'办理中');
				}
			});
			this.socket.on('closeFileCallback?'+that.userId, function(data){
				if(data.success){
					that.infopend(data.msg);
				}
			});
			
			this.eventBindList[this.userId] = true;
		},
		close:function(){
			if(this.userId){
				var obj = {
					fileId: this.fileId,
					fileName: this.fileName,
					userId: this.userId,
					userName: this.userName
				};
				this.socket.emit('closeFile', obj);
			}
		},
		getUUid: function() {
			return new Date().getTime() + "" + Math.floor(Math.random() * 899 + 100);
		}
	};
})();